﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AGDN;

namespace Sudum
{
    class BulletManager
    {
        public static BulletManager Me { set; get; }
        List<Bullet> bullets;
        List<Bullet> bulletsToRemove = new List<Bullet>();
        Model bulletModel;
        GameLevel associatedGameLevel;

        public BulletManager(GameLevel gl)
        {
            Me = this;
            bullets = new List<Bullet>();
            associatedGameLevel = gl;
            bulletModel = Sudum.Me.Content.Load<Model>("Models\\Sphere");
        }

        public void Update()
        {            
            foreach (Bullet b in bullets)
            {
                if (b.IsAlive())
                    b.Update();
                else
                    bulletsToRemove.Add(b);
            }

            foreach (Bullet b in bulletsToRemove)
            {
                bullets.Remove(b);
            }

            bulletsToRemove.Clear();
        }

        void DrawBullet(Vector3 pos, Matrix[] trans)
        {
            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in bulletModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    
                    effect.VertexColorEnabled = true;
                    effect.TextureEnabled = false;
                    effect.DiffuseColor = Color.White.ToVector3();

                    // apply world transformation here
                    effect.World = trans[mesh.ParentBone.Index] * Matrix.CreateTranslation(pos) * Matrix.CreateScale(0.8f) * Matrix.CreateRotationX(associatedGameLevel.GetTubeRotationX());

                    // set current view/projection
                    effect.View = associatedGameLevel.GetLevelCamera().GetView();
                    effect.Projection = associatedGameLevel.GetLevelCamera().GetProjection();
                }

                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }

        public void Draw()
        {
            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[bulletModel.Bones.Count];
            bulletModel.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (Bullet b in bullets)
            {
                DrawBullet(b.GetPosition(), transforms);
            }
        }

        public void CreateBullet(Vector3 pos, Vector3 dir, BulletSource bs)
        {
            Bullet b = new Bullet(pos, dir, bs);
            bullets.Add(b);
        }

        public void RemoveBullet(Bullet b)
        {
            bullets.Remove(b);
        }
        public List<Bullet> GetBullets()
        {
            return bullets;
        }
        
    }
}
